/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 CallbackHolder.hpp. Created on: 20.11.2009 12:29:28 
 */

#ifndef CALLBACKHOLDER_HPP_
#define CALLBACKHOLDER_HPP_

#include "smartobjects/SmartObjectsDefs.hpp"

namespace smartobjects {

class CallbackHolderBase : public RefObject {
public:
	inline CallbackHolderBase() {}
	inline virtual ~CallbackHolderBase() {}
	virtual bool hasMethodCallback() = 0;
};

template<typename IC, typename MC = boost::function< void () > >
class CallbackHolder : public CallbackHolderBase {	
public:
	inline CallbackHolder() : m_poInterfaceCallback(NULL), m_pfMethodCallback(NULL), m_poUserObject(NULL) {
	}

	inline virtual ~CallbackHolder() {
		releaseAndClear(m_poInterfaceCallback);
		releaseAndClear(m_poUserObject);
	}

	inline const MC& getMethodCallback() const {
		return m_pfMethodCallback;
	}

	inline void setMethodCallback(MC pfMethodCallback) {
		m_pfMethodCallback = pfMethodCallback;
	}

	inline virtual bool hasMethodCallback() {
		return !m_pfMethodCallback.empty();
	}

	inline IC* getInterfaceCallback() const {
		return m_poInterfaceCallback;
	}

	inline void setInterfaceCallback(IC* poInterfaceCallback) {
		setNewRefObject(m_poInterfaceCallback, poInterfaceCallback);
	}

	inline RefObject* getUserObject() const {
		return m_poUserObject;
	}

	inline void setUserObject(RefObject* poUserObject) {
		setNewRefObject(m_poUserObject, poUserObject);
	}

	inline const std::string& getOperationName() const {
		return m_sOperationName;
	}

	inline void setOperationName(const std::string& sName) {
		m_sOperationName = sName;
	}

protected:
	IC* m_poInterfaceCallback;
	MC m_pfMethodCallback;
	RefObject* m_poUserObject;
	std::string m_sOperationName;
};


}

#endif /* CALLBACKHOLDER_HPP_ */
